package util;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

public class ImgUtil {
	public static String imgUpload(HttpServletRequest req) {
		String filename = null;
		Part part;
		try {
			part = req.getPart("file");
			if (part.getSize() == 0)
				return null;
			String name = part.getHeader("content-disposition");

			String root = req.getServletContext().getRealPath("/client/images/ad");

			String str = name.substring(name.lastIndexOf("."), name.length() - 1);

			filename = UUID.randomUUID().toString() + str;

			part.write(root + "\\" + filename);

			return filename;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
